<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划日期" prop="planDate">
        <el-date-picker clearable
                        v-model="queryParams.planDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择计划日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="截止日期" prop="materialDeadline">
        <el-date-picker clearable
                        v-model="queryParams.materialDeadline"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择物料需求截止日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="approvalStatus">
        <el-select v-model="queryParams.approvalStatus" placeholder="请选择状态" clearable>
          <el-option label="草稿" value="0"/>
          <el-option label="已审批" value="1"/>
          <el-option label="已完成" value="2"/>"
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="采购计划编号" align="center" prop="planNumber">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['cg:plan:info']"
          >{{scope.row.planNumber}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="采购计划名称" align="center" prop="planName"/>
      <el-table-column label="采购计划日期" align="center" prop="planDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物料需求截止日期" align="center" prop="materialDeadline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.materialDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks"/>
      <el-table-column label="状态" align="center" prop="approvalStatus">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.approvalStatus === 0">草稿</el-tag>
          <el-tag type="warning" v-else-if="scope.row.approvalStatus === 1">已审批</el-tag>
          <el-tag type="success" v-else-if="scope.row.approvalStatus === 2">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip content="审核" placement="top">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-s-check"
              @click="handleView(scope.row)"
              v-hasPermi="['cg:plan:check']"
            >
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改采购计划对话框 -->
      <add ref="add"></add>
    <!--查看/审核采购计划对话框 -->
     <approveView ref="infoView"></approveView>
<!--      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>-->
  </div>
</template>

<script>
import {listApprovePlan, delPlan} from "@/api/mes/cg/plan";

import Add from "@/views/mes/cg/plan/add.vue";
import approveView from "@/views/mes/cg/plan/approveView.vue";


export default {
  name: "Plan",
  components: {
    Add,
    approveView
  },
  dicts: ['erp_payment_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planName: null, planDate: null, materialDeadline: null, approvalStatus: null,
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatCurrency(number) {
      return new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY' }).format(number);
    },
    /** 查询采购计划列表 */
    getList() {
      this.loading = true;
      listApprovePlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleView(row) {
      this.$refs.infoView.open=true;
      this.$refs.infoView.title=row.approvalStatus===0?'审核采购计划':'查看采购计划';
      this.$refs.infoView.init(row.planId);
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('cg/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
